<template>
  <div class="cmt-container">
    <h3>发表评论</h3>
    <hr />
    <textarea placeholder="请输入要BB的内容（做多吐槽120字）" maxlength="120"></textarea>

    <mt-button type="primary" size="large">发表评论</mt-button>

    <div class="cmt-list">
      <div class="cmt-item" v-for="(item, i) in comments" :key="i">
        <div
          class="cmt-title"
        >第{{ i+1 }}楼&nbsp;&nbsp;用户：{{ item.user_name }}&nbsp;&nbsp;发表时间：{{ item.add_time | dateFormat }}</div>
        <div class="cmt-body">{{ item.content || '此用户很懒，嘛都没说' }}</div>
      </div>
    </div>

    <mt-button v-if="isMore" type="danger" size="large" plain @click="getMore">加载更多</mt-button>
    <p class="not-more" v-if="!isMore">人家是有底线的~~</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      comments: [],
      pageIndex: 1,
      isMore: true
    };
  },
  props: {
    id: {
      type: String,
      validator(value) {
        return parseInt(value) > 0;
      }
    },
    url: {
      type: String
    }
  },
  created() {
    this.getData();
  },
  methods: {
    getData() {
      this.$http
        .get(this.url + "/" + this.id + "?pageindex=" + this.pageIndex)
        .then(result => {
          if (result.body.status === 0) {
            //这里默认每次取10条，如果返回的数据长度小于10
            //相当于最后一页， 不再显示获取更多按钮
            this.comments = this.comments.concat(result.body.message);
            if (result.body.message.length < 10) {
              this.isMore = false;
            } else {
              this.pageIndex++;
            }
          } else {
            this.$toast("评论获取失败:" + result.body.message);
          }
        });
    },
    getMore(){
        this.getData()
    }
  }
};
</script>

<style lang="less" scoped>
.cmt-container {
  h3 {
    font-size: 18px;
  }
  textarea {
    font-size: 14px;
    height: 85px;
    margin: 0;
  }

  .cmt-list {
    margin: 5px 0;
    .cmt-item {
      font-size: 13px;
      .cmt-title {
        line-height: 30px;
        background-color: #ccc;
      }
      .cmt-body {
        line-height: 35px;
        text-indent: 2em;
      }
    }
  }
  .not-more{
      font-size: 14px;
      color: #ccc;
      text-align: center;
  }
}
</style>